#include "StdAfx.h"
#include "CorelStackTraceMgr.h"

DWORD CCorelStackTraceMgr::GetStackTraceId(LPCWSTR lpcwszStackTrace)
{
    DWORD t_dwTID = (DWORD)-1;
    std::map<std::wstring, DWORD>::iterator itr;
    EnterCriticalSection(&m_csLock);

    itr = m_mapStackTraceRepository.find(lpcwszStackTrace);
    if (m_mapStackTraceRepository.end() == itr)
    {
        size_t t_dwCount = m_mapStackTraceRepository.size();
        m_mapStackTraceRepository.insert(std::make_pair(lpcwszStackTrace, t_dwCount));
        itr = m_mapStackTraceRepository.find(lpcwszStackTrace);
        t_dwTID = itr->second;
    }

    LeaveCriticalSection(&m_csLock);

    return t_dwTID;
}

BOOL  CCorelStackTraceMgr::GetStackTrace(DWORD dwStackTraceId, std::wstring& wstrStackTrace)
{
    BOOL t_bRet = FALSE;

    EnterCriticalSection(&m_csLock);

    std::map<std::wstring, DWORD>::iterator iter = m_mapStackTraceRepository.begin();

    while (iter != m_mapStackTraceRepository.end()) {
        if (iter->second == dwStackTraceId) {
            wstrStackTrace = iter->first;
            t_bRet = TRUE;
            break;
        }
        ++iter;
    }

    LeaveCriticalSection(&m_csLock);

    return t_bRet;
}

VOID CCorelStackTraceMgr::Clear()
{
    EnterCriticalSection(&m_csLock);
    m_mapStackTraceRepository.clear();        
    LeaveCriticalSection(&m_csLock);
}